package nio;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

/**
 * 通道之间数据传输
 *
 * Created by Zhou_Bing on 2018/9/26.
 */
public class ChannelTransfer {

    public static void main(String[] args) throws IOException {

        String relativePath = System.getProperty("user.dir");
        String[] iF = new String[]{"D:/test1.txt","D:/test2.txt","D:/test3.txt","D:/test4.txt"};
        String oF = "D:/test_out.txt";

        FileOutputStream fos = new FileOutputStream(oF);
        WritableByteChannel targetChannel = fos.getChannel();
        for(int i=0;i<iF.length;i++){

            FileInputStream fis = new FileInputStream(iF[i]);
            FileChannel sourceChannel = fis.getChannel();
            sourceChannel.transferTo(0,sourceChannel.size(),targetChannel);

            sourceChannel.close();
            fis.close();
        }
        targetChannel.close();
        fos.close();
        System.out.println("All jobs done....");
    }
}
